﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Way.Bus.Controllers;
using Way.Bus.Wms.Model;
using Way.Contract.MVC;
using Way.Contract.Data;
using Way.Contract.Data.ControllerData;
using Way.EAP.DataAccess.Data;
using Way.EAP.DataAccess.Regulation;

namespace Way.Bus.Wms.Controllers.BaseData
{
    /// <summary>
    /// 客户商品货位关系配置管理
    /// </summary>
    public class DepotwbsController : BusinessBaseController
    {
        public DepotwbsController() : base(new DepotwbsManage()) { }
        public DepotwbsController(IModel model) : base(model) { }

        public override bool IsImportIn
        {
            get
            {
                return false;
            }
        }
        protected override void OnInitViewChildItem(ModelData data, ItemData item)
        {
            base.OnInitViewChildItem(data, item);
            if (item.IsField("CODE") || item.IsField("ISDISABLE")
                || item.IsField("CREATEDBY") || item.IsField("CREATIONDATE")
                || item.IsField("REMARK"))
                item.visible = false;
        }
        protected override ActionCommand[] OnInitCommand(ActionCommand[] commands)
        {
            ActionCommand[] ac = base.OnInitCommand(commands);
            foreach (ActionCommand cmd in ac)
            {
                if (cmd.command == "add" || cmd.command == "edit" || cmd.command == "StartDisable")
                    cmd.visible = false;
                if (cmd.command == "remove")
                    cmd.name = "删除货位配置";

            }
            return ac;
        }
        protected override void OnForeignLoading(IModel model, loadItem item)
        {
            List<SQLWhere> list = new List<SQLWhere>();
            //物流公司与客户是同一张表，ftype=1是物流公司 ftype=2是客户
            if (base.fdata.filedname == "KID")
            {
                list.AddRange(item.whereList);
                SQLWhere w = new SQLWhere();
                w.ColumnName = "ftype";
                w.Value = "2";
                list.Add(w);
                item.whereList = list.ToArray();
            }
        }
       /// <summary>
       /// 配置完成的客户货位对应集合
       /// </summary>
        [WhereParameter]
        public DepotwbsManage[] dm { get; set; }

        [ActionCommand(name = "保存货位配置", title = "保存已选择的客户与货位关系配置", index = 1, icon = "icon-save", isalert = false, onclick = "saveconfig",isselectrow=false)]
        public void SaveConfig()
        {
            if (dm != null)
            {
                string tn = this.model.ToString();
                IDataAction action = this.model.GetDataAction();
                string sql = "select id from {0} where KID={1} and LID={2} and WID={3} and DID={4}";
                string ins = "insert into {0} (KID,LID,WID,DID) values ({1},{2},{3},{4})";
                foreach (DepotwbsManage d in dm)
                {
                    object o = action.GetValue(string.Format(sql, tn, d.KID, d.LID, d.WID, d.DID));
                    if (o != null && !string.IsNullOrEmpty(o.ToString()))
                    {
                        // throw new Exception("");
                    }
                    else
                        action.Execute(string.Format(ins, tn, d.KID, d.LID, d.WID, d.DID));
                }
            }
        }

        protected override void Remove()
        {
            if (string.IsNullOrEmpty(reductionid))
                base.Remove();
            else
                base.ClearAway();
        }
    }
}
